package com.sixth.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.nacos.common.utils.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sixth.core.entity.CareOrderItem;
import com.sixth.core.entity.CheckResult;
import com.sixth.core.vo.R;
import com.sixth.mapper.CheckResultMapper;
import com.sixth.service.CheckResultService;
import com.sixth.vo.CheckResultVo;
import com.sixth.vo.ResultEntryVo;
import com.sixth.vo.ResultImgVo;
import com.sixth.vo.UpdateResultEntryVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @program: sixth-hospital-parent
 * @description:
 * @author: 李劲锋
 * @create: 2024-08-22 10:25
 **/
@Service
public class CheckResultServiceImpl extends ServiceImpl<CheckResultMapper, CheckResult> implements CheckResultService {
    @Autowired
    private CheckResultMapper checkResultMapper;
    @Override
    public R<IPage<CheckResult>> selectByPage(ResultEntryVo resultEntryVo) {
        // 创建一个分页对象
        IPage<CheckResult> page = new Page<>(resultEntryVo.getPageNum(), resultEntryVo.getPageSize());

        // 创建一个查询对象
        QueryWrapper<CheckResult> wrapper = new QueryWrapper<>();

        // 添加查询条件
        if (StringUtils.hasText(resultEntryVo.getRegId())) {
            wrapper.eq("reg_id", resultEntryVo.getRegId().trim());
        }
        if (resultEntryVo.getPatientName() != null && !resultEntryVo.getPatientName().trim().isEmpty()) {
            wrapper.eq("patient_name", resultEntryVo.getPatientName().trim());
        }
        if (resultEntryVo.getCheckItemIds() != null && !resultEntryVo.getCheckItemIds().isEmpty()) {
            wrapper.in("check_item_id", resultEntryVo.getCheckItemIds());
        }

        wrapper.orderByDesc("create_time");
        wrapper.eq("result_status", "0");

        // 执行分页查询
        IPage<CheckResult> checkPage =  checkResultMapper.selectPage(page, wrapper);
        return R.ok(checkPage);
    }

    @Override
    public R<CheckResult> startCheckUpdate(UpdateResultEntryVo updateResultEntryVo) {

        // 打印接收到的数据
        System.out.println("Received UpdateResultEntryVo: " + updateResultEntryVo);
        // 检查传入的参数是否为空
        if (updateResultEntryVo == null || updateResultEntryVo.getItemId() == null) {
            return R.error("更新失败，参数不完整");
        }

        QueryWrapper<CheckResult> wrapper = new QueryWrapper<>();
        wrapper.eq("coc_id", updateResultEntryVo.getItemId());

        CheckResult checkResult = new CheckResult();
        checkResult.setResultMsg(updateResultEntryVo.getResultMsg());

        // 将 resultImg 对象列表转换为字符串列表（将 url 和 name 属性拼接在一起）
        String jsonString = JSON.toJSONString(updateResultEntryVo.getResultImg());

        checkResult.setResultImg(jsonString);  // 设置转换后的字符串列表
        checkResult.setResultStatus("1");
        checkResult.setUpdateTime(new Date());

        // 执行更新操作
        int updateCount = checkResultMapper.update(checkResult, wrapper);

        // 检查是否更新成功
        if (updateCount > 0) {
            return R.ok(checkResult);  // 返回的是传入的 checkResult 对象，注意它并非是数据库中的最新状态
        } else {
            return R.error("更新失败，未找到对应记录");
        }
    }

    @Override
    public R<IPage<CheckResult>> listByPageResult(ResultEntryVo resultEntryVo) {
        // 创建一个分页对象
        IPage<CheckResult> page = new Page<>(resultEntryVo.getPageNum(), resultEntryVo.getPageSize());

        // 创建一个查询对象
        QueryWrapper<CheckResult> wrapper = new QueryWrapper<>();

        // 添加查询条件
        if (StringUtils.hasText(resultEntryVo.getRegId())) {
            wrapper.eq("reg_id", resultEntryVo.getRegId().trim());
        }
        if (resultEntryVo.getPatientName() != null && !resultEntryVo.getPatientName().trim().isEmpty()) {
            wrapper.eq("patient_name", resultEntryVo.getPatientName().trim());
        }
        if (resultEntryVo.getCheckItemIds() != null && !resultEntryVo.getCheckItemIds().isEmpty()) {
            wrapper.in("check_item_id", resultEntryVo.getCheckItemIds());
        }

        wrapper.orderByDesc("create_time");
        wrapper.eq("result_status", "1");

        // 执行分页查询
        IPage<CheckResult> checkPage =  checkResultMapper.selectPage(page, wrapper);
        return R.ok(checkPage);
    }

}
